package com.sky.wsp.tong.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.wsp.tong.dao.JobMapper;
import com.sky.wsp.tong.dao.vo.JobVO;
import com.sky.wsp.tong.util.WebDriverUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class ScribeZhiLianController {

    @Resource
    private JobMapper jobMapper;

    private String[] getSearchKeys() {
        return new String[]{
                "k8s",
                "docker",
//                "paas",
//                "容器技术",
//                "云原生",
                "kubernetes",
                "go运维",
                "自动化运维",
                "devops"
//                "elk",
//                "jenkins",
//                "ci/cd",
//                "容器云",
//                "微服务",
//                "springcloud",
//                "git",
//                "gitlab",
//                "jira",
//                "自动化流水线"
        };
    }

    @GetMapping("/zhilian")
    public String zhiLian() throws Exception {
        String[] searchKeys = getSearchKeys();
//        String city = "530"; // 北京
//        String city = "538"; // 上海
//        String city = "763"; // 广州
        int beginCity = 635;
        int endCity = 748;
        if(beginCity < 530){
            return "智联的国内城市码重530开始，不允许小于530";
        }else if(beginCity > endCity){
            return "系统默认设置的城市截止码为" + endCity + "，不允许超过截止码";
        }
        for (int i = beginCity; i < endCity; i++) {
            String city = i + "";
            for (String searchKey : searchKeys) {
                int currentPage = 1;
                int nextPage = 1;
                try {
                    nextPage = scribe(searchKey, city, currentPage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                while (nextPage > currentPage) {
                    currentPage = nextPage;
                    // 间歇1-5秒
                    Thread.sleep(RandomUtil.randomLong(1000, 5000));
                    try {
                        nextPage = scribe(searchKey, city, currentPage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        WebDriverUtil.driverQuit();
        return "OK";
    }

    /**
     * 抓取本页数据并返回下页页码
     */
    private int scribe(String searchKey, String city, int currentPage) throws Exception {
        // in=100000000 IT行业
        String url = "https://sou.zhaopin.com/?in=100000000&jl=" + city + "&kw=" + searchKey + "&p=" + currentPage;
//        String url = "https://sou.zhaopin.com/?in=100000000&jl=530&kw=devops&p=1";
        System.out.println("begin scribe: " + url);
        WebDriver driver = WebDriverUtil.getDriver();
        driver.get(url);
        WebElement element = driver.findElement(By.className("positionlist"));
        WebElement pagination = element.findElement(By.className("pagination"));

        List<WebElement> positions = element.findElements(By.className("joblist-box__item"));
        for (WebElement p : positions) {
            WebElement link = p.findElement(By.tagName("a"));
            String detailUrl = link.getAttribute("href");
            String jobname = link.findElement(By.className("iteminfo__line1__jobname__name")).getText();
            String compname = link.findElement(By.className("iteminfo__line1__compname__name")).getText();
            List<WebElement> compdesc = link.findElements(By.className("iteminfo__line2__compdesc__item"));
            String scale = "";
            String stage = "";
            if (compdesc.size() > 0) {
                stage = compdesc.get(0).getText();
                if (compdesc.size() > 1) {
                    scale = compdesc.get(1).getText();
                }
            }
            String salary = link.findElement(By.className("iteminfo__line2__jobdesc__salary")).getText();
            List<WebElement> demands = link.findElements(By.className("iteminfo__line2__jobdesc__demand__item"));
            String age = "";
            String education = "";
            if (demands.size() > 0) {
                age = demands.get(0).getText();
                if (demands.size() > 1) {
                    education = demands.get(1).getText();
                }
            }

            JobVO job = JobVO.JobVOBuilder.jobVO()
                    .withJobName(jobname)
                    .withUrl(detailUrl)
                    .withIndustry("")
                    .withCompany(compname)
                    .withScale(scale)
                    .withStage(stage)
                    .withSalary(salary)
                    .withAge(age)
                    .withEducation(education)
                    .withDuty("")
                    .build();

            jobMapper.insert(job);
            System.err.println(job);
        }

        List<WebElement> soupager = pagination.findElements(By.className("soupager__index"));
        WebElement maxPage = soupager.get(soupager.size() - 1);
        String text = maxPage.getText();
        if("...".equals(text)){
            // 还有更多页数据
            maxPage = soupager.get(soupager.size() - 2);
            text = maxPage.getText();
        }
        if(!StrUtil.isNumeric(text)){
            // 最后一页
            return currentPage;
        }
        int maxNum = Integer.valueOf(text);
        int nextPage = currentPage + 1;
        return nextPage > maxNum ? maxNum : nextPage;
    }


    @GetMapping("/zhilian/detail")
    public String zhiLianDetail() {
        IPage<JobVO> page = new Page<>();
        page.setSize(100);
        LambdaQueryWrapper<JobVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JobVO::getDuty, "");
        IPage<JobVO> jobVOIPage = jobMapper.selectPage(page, wrapper);
        while (true) {
            List<JobVO> records = jobVOIPage.getRecords();
            if (records.isEmpty()) {
                break;
            }
            for (JobVO job : records) {
                String url = job.getUrl();
                try {
                    scribeDetail(url, job);
                } catch (Exception e) {
                    e.printStackTrace();
                    job.setDuty("1");
                }
                jobMapper.updateById(job);
            }
            // 下一页
            page.setCurrent(page.getCurrent() + 1);
            jobVOIPage = jobMapper.selectPage(page, wrapper);
        }

        return "OK";
    }

    private void scribeDetail(String url, JobVO job) throws Exception {
        Thread.sleep(RandomUtil.randomLong(1000, 2000));
        WebDriver driver = WebDriverUtil.getDriver();
        driver.get(url);
        WebElement detail = driver.findElement(By.className("job-detail"));
        WebElement duty = detail.findElement(By.className("describtion__detail-content"));
        WebElement address = detail.findElement(By.className("job-address__content-text"));

        WebElement publisher = driver.findElement(By.className("publisher"));
        WebElement industry = publisher.findElement(By.className("company__industry"));

        job.setIndustry(industry.getText());
        job.setLocation(address.getText());
        job.setDuty(duty.getText());
    }
}
